package de.backtomedieval.activities.startscreen;


import de.backtomedieval.game.KI;
import de.backtomedieval.game.Player;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import de.backtomedieval.R;
import de.backtomedieval.activities.gamemenu.Gamemenu;
import de.backtomedieval.main.Game;

public class NewProfile extends Activity {
	

	private GridView grid_main;



	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
		setContentView(R.layout.startscreen_newgame);       



		Spinner s = (Spinner) findViewById(R.id.newgame_gamespeed);
		ArrayAdapter<?> adapter;
		if(!Game.DEBUG) {
			adapter = ArrayAdapter.createFromResource(this, R.array.gamespeed, android.R.layout.simple_spinner_item);
		} else {
			adapter = ArrayAdapter.createFromResource(this, R.array.gamespeed_debug, android.R.layout.simple_spinner_item);
		}				
		adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
		s.setAdapter(adapter);
		
		Spinner sdiff = (Spinner) findViewById(R.id.newgame_difficulty);
		ArrayAdapter<?> diffadapter = ArrayAdapter.createFromResource(
				this, R.array.difficulty, android.R.layout.simple_spinner_item);
		diffadapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
		sdiff.setAdapter(diffadapter);

		// Restore preferences
		final EditText editPlayername = (EditText) findViewById(R.id.newgame_playername);
		final EditText editCityname = (EditText) findViewById(R.id.newgame_cityname);
		SharedPreferences settings = getSharedPreferences(Game.PREFS_NAME, 0);
		boolean pauseOnEndOfFight = settings.getBoolean("pauseonendoffight", false);
		Game.setPauseOnEndOfFight(pauseOnEndOfFight);
		boolean pauseOnStartOfFight = settings.getBoolean("pauseonstartoffight", false);
		Game.setPauseOnStartOfFight(pauseOnStartOfFight);
		boolean pauseOnGameStart = settings.getBoolean("pauseongamestart", false);
		Game.setPauseOnGameStart(pauseOnGameStart);
		
		String playername = settings.getString("playername", "Player");
		editPlayername.setText(playername);
		String homecityname = settings.getString("homecityname", "Nuremberg");
		editCityname.setText(homecityname);
		int loadGameSpeed = settings.getInt("gamespeed", 2);
		if (loadGameSpeed>5) {
			loadGameSpeed=5;
		}
		s.setSelection(loadGameSpeed);
		int loadDiff = settings.getInt("difficulty", 2);
		sdiff.setSelection(loadDiff);		
		grid_main = (GridView)findViewById(R.id.GridViewMenu);
		grid_main.setAdapter(new ImageAdapter(this));
        grid_main.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
            	onNewGameMenuChoose(position);
            }
        });

		
	}


	
	
	
	protected void onNewGameMenuChoose(int position) {
		if (position==0) {
			onButtonStart();
		}
	}





	@Override
	protected void onStop(){
		super.onStop();

		// We need an Editor object to make preference changes.
		// All objects are from android.context.Context
		SharedPreferences settings = getSharedPreferences(Game.PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		final EditText editPlayername = (EditText) findViewById(R.id.newgame_playername);
		final EditText editCityname = (EditText) findViewById(R.id.newgame_cityname);
		final Spinner s = (Spinner) findViewById(R.id.newgame_gamespeed);
		final Spinner sdiff = (Spinner) findViewById(R.id.newgame_difficulty);
		editor.putString("playername", editPlayername.getText().toString());
		editor.putString("homecityname", editCityname.getText().toString());
		editor.putInt("gamespeed", s.getSelectedItemPosition());
		editor.putInt("difficulty", sdiff.getSelectedItemPosition());
		// Commit the edits!
		editor.commit();
	}

	public void onButtonStart() {
		final EditText editPlayername = (EditText) findViewById(R.id.newgame_playername);
		final EditText editCityname = (EditText) findViewById(R.id.newgame_cityname);
		final Spinner s = (Spinner) findViewById(R.id.newgame_gamespeed);
		final Spinner sdiff = (Spinner) findViewById(R.id.newgame_difficulty);
		
		Game.setGameSpeed(s.getSelectedItemPosition());
		Game.setDifficulty(sdiff.getSelectedItemPosition());
		Game.setPlayername(editPlayername.getText().toString());
		Game.setCityname(editCityname.getText().toString());
		
		/*
		if (playername.equals("ki0")) {
			player.setKI(new KI(player,0));
		}else if (playername.equals("ki1")) {
			player.setKI(new KI(player,1));
		}if (playername.equals("ki2")) {
			player.setKI(new KI(player,2));
		}if (playername.equals("ki3")) {
			player.setKI(new KI(player,3));
		}if (playername.equals("ki4")) {
			player.setKI(new KI(player,4));
		}if (playername.equals("ki5")) {
			player.setKI(new KI(player,5));
		} */
		

		Intent profilemenuIntent = new Intent(NewProfile.this, ProfileStartscreen.class);
		startActivity(profilemenuIntent);
		this.finish();
	}


	@Override
	public void onBackPressed() {
		Intent changeToIntent = new Intent(this,Startscreen.class);
		startActivity(changeToIntent);
		this.finish();
		return;
	}
	
	public class ImageAdapter extends BaseAdapter{
		Context mContext;
		public static final int ACTIVITY_CREATE = 10;
		public ImageAdapter(Context c){
			mContext = c;
		}

		public int getCount() {
			return 1;
		}


		public View getView(int position, View convertView, ViewGroup parent) {
			View v;
			if(convertView==null){
				LayoutInflater li = getLayoutInflater();
				v = li.inflate(R.layout.gridiconlarge, null);
				TextView tv = (TextView)v.findViewById(R.id.icon_text);
				ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
				tv.setText(R.string.play);
				iv.setImageResource(R.drawable.icon);

			}
			else{
				v = convertView;
			}
			return v;
		}
		
		
		
		public Object getItem(int position) {
			return null;
		}
		
		public long getItemId(int position) {
			return 0;
		}
	}

}
